package com.module.admin.rest.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.module.admin.rest.dao.RestStatDayDao;
import com.module.admin.rest.pojo.RestInfo;
import com.module.admin.rest.pojo.RestServer;
import com.module.admin.rest.pojo.RestStatDay;
import com.module.admin.rest.service.RestInfoService;
import com.module.admin.rest.service.RestServerService;
import com.module.admin.rest.service.RestStatDayService;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameNoUtil;
import com.system.comm.utils.FrameStringUtil;
import com.system.handler.model.ResponseCode;
import com.system.handler.model.ResponseFrame;

/**
 * rest_stat_day的Service
 * @author yuejing
 * @date 2016-12-26 08:46:41
 * @version V1.0.0
 */
@Component
public class RestStatDayServiceImpl implements RestStatDayService {

	@Autowired
	private RestStatDayDao restStatDayDao;
	@Autowired
	private RestInfoService restInfoService;
	@Autowired
	private RestServerService restServerService;
	
	@Override
	public ResponseFrame saveOrUpdate(RestStatDay restStatDay) {
		ResponseFrame frame = new ResponseFrame();
		RestStatDay org = restStatDayDao.getRiIdStatDate(restStatDay.getRiId(), restStatDay.getStatDate());
		if(org == null) {
			if(FrameStringUtil.isEmpty(restStatDay.getRsdId())) {
				restStatDay.setRsdId(FrameNoUtil.uuid());
			}
			restStatDayDao.save(restStatDay);
		} else {
			restStatDay.setRsdId(org.getRsdId());
			restStatDayDao.update(restStatDay);
		}
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	@Override
	public RestStatDay get(String rsdId) {
		return restStatDayDao.get(rsdId);
	}

	@Override
	public ResponseFrame pageQuery(RestStatDay restStatDay) {
		ResponseFrame frame = new ResponseFrame();
		int total = restStatDayDao.findRestStatDayCount(restStatDay);
		List<RestStatDay> data = null;
		if(total > 0) {
			data = restStatDayDao.findRestStatDay(restStatDay);
			for (RestStatDay rsd : data) {
				RestInfo ri = restInfoService.get(rsd.getRiId());
				rsd.setUrl(ri.getUrl());
				RestServer rs = restServerService.get(rsd.getRsId());
				rsd.setServerAddress(rs.getServerAddress());
			}
		}
		Page<RestStatDay> page = new Page<RestStatDay>(restStatDay.getPage(), restStatDay.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	@Override
	public ResponseFrame delete(String rsdId) {
		ResponseFrame frame = new ResponseFrame();
		restStatDayDao.delete(rsdId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
}
